/*Copyright (c) Penrillian Ltd 2003-2006. All rights reserved. Web: www.penrillian.com*/

#include "TestModule.h"
#include "TestDriver.h"
#include "Logger.h"
#include "TStringItem.h"
#include "Service.h"
#define UNIT_TEST
#include "Client.h"

void TestModule::setUp()
{
    
}

void TestModule::tearDown()
{
}

_LIT(KOneWorld, "Yes,HelloWorld");
void TestModule::testTStringItem()
{
	TStringItem* item = TStringItem::NewL(KOneWorld);
	TDesC* des = item->toString();  
	TS_ASSERT_EQUALS_DESCRIPTOR(*des, KOneWorld);
	TUint type = item->Type();
	TS_ASSERT_EQUALS(type, 0);
	TS_ASSERT_EQUALS(item->Size(), sizeof(TItem) + KOneWorld().Length()*sizeof(TUint16));
	
	TUint8* itemCopy = (TUint8*)User::AllocL(item->Size());
	item->Copy(itemCopy);
	TStringItem* itemCopyString = (TStringItem*)itemCopy;
	TS_ASSERT(item->Equal(*itemCopyString));
	
	delete des;
	delete item;
	delete itemCopyString;
	
	TStringItem* item2 = TStringItem::NewL(KNullDesC);
	TDesC* des2 = item2->toString();
	TS_ASSERT_EQUALS_DESCRIPTOR(*des2, KNullDesC);
	delete des2;
	delete item2;
}

_LIT(KStackName, "StackModule");
void TestModule::testCallStackInit()
{
    /*
     * 测试创建全局块创建的初始化是否全部正确
     */
    RCallStack iStack;
    iStack.CreateL(KStackName, 1024 * 4);
    TFindChunk chunk;
    chunk.Find(KStackName);
    TFullName name;
    TInt code = chunk.Next(name);  
    TS_ASSERT(code == KErrNone);
    TS_ASSERT_EQUALS_DESCRIPTOR(name, KStackName);
    TS_ASSERT(iStack.Count() == 0);
    TS_ASSERT(iStack.isEmpty());
    TS_ASSERT_EQUALS(iStack.Size(), 1024*4);
     
    /*
     * 测试创建同样的全局块是否会抛出异常
     */
    RCallStack sameStack;
    TS_ASSERT_THROWS(sameStack.CreateL(KStackName, 1024*4), KErrAlreadyExists);
    
    /*
     * 测试打开后一些列参数是否符合期望
     */
    sameStack.OpenL(KStackName);
    TS_ASSERT(sameStack.IsAvaliable());
    TS_ASSERT_EQUALS(iStack.Size(), sameStack.Size());
    TS_ASSERT_EQUALS(iStack.Count(), sameStack.Count());
    
    /**
     * 全部关闭后应该没有该chunk块
     */
    sameStack.Close();
    TS_ASSERT(!sameStack.IsAvaliable());
    iStack.Close();
    TFindChunk chunkClose;
    chunkClose.Find(KStackName);
    code = chunkClose.Next(name);
    TS_ASSERT(code != KErrNone);
}

void TestModule::testPanicServer()
{
    
}

void TestModule::testClient()
{
    _LIT(KServer1, "Server1");
    _LIT(KServer2, "Server2");
    _LIT(KServer3, "Server3");
    _LIT(KServer4, "Server4");
    
    CClient* one = CClient::InstanceL(KServer1, 4096);
    TS_ASSERT(one != NULL); 
    TS_ASSERT(CClient::Count() == 1);
    
    CClient* two = CClient::InstanceL(KServer2, 4096);
    TS_ASSERT(two != NULL);
    TS_ASSERT(CClient::Count() == 2);
    
    CClient* findTwo = CClient::InstanceL(KServer2, 4096);
    TS_ASSERT(two == findTwo);
    
    CClient* three = CClient::InstanceL(KServer3, 4096);
    TS_ASSERT(three != NULL);
    TS_ASSERT(CClient::Count() == 3);
    
    CClient* four = CClient::InstanceL(KServer4, 4096);
    TS_ASSERT(four != NULL);
    TS_ASSERT(CClient::Count() == 4);
    TS_ASSERT_THROWS(CClient::InstanceL(_L("MoreStack"), 4096), KErrFull);
    
    CClient::CloseInstance(KServer2);
    TS_ASSERT(three == CClient::GetIndex(1));
    TS_ASSERT(four == CClient::GetIndex(2));
    TS_ASSERT(CClient::Count() == 3);
    
    CClient::CloseInstance(KServer2);
    
    CClient::FreeAllInstance();
}

void TestModule::testSize()
{
    TS_ASSERT(4 == AlignSize(1));
    TS_ASSERT(4 == AlignSize(2));
    TS_ASSERT(4 == AlignSize(3));
    TS_ASSERT(4 == AlignSize(4));
    TS_ASSERT(8 == AlignSize(5));
    TS_ASSERT(12 == AlignSize(11));
    TS_ASSERT(32 == AlignSize(29));
    TS_ASSERT(1024 == AlignSize(1023));
    TS_ASSERT(1024 == AlignSize(1024));
}

